package cn.prophet.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.prophet.bean.User;
import cn.prophet.bean.UserExample;
import cn.prophet.bean.UserExample.Criteria;
import cn.prophet.mapper.UserMapper;
import cn.prophet.service.LoginService;
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Integer startRegistered(String username, String psw) {
        UserExample userExample = new UserExample();
        Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(username);
        List<User> userList = userMapper.selectByExample(userExample);
        if(userList!=null&&userList.size()>0) {
            return 0;
        }
        //如果这个用户没注册过，开始注册
        User user = new User();
        user.setUserName(username);
        user.setUserPsw(psw);
        user.setCreateTime(new Date());
        user.setBalance(new BigDecimal(1000));
        int record = userMapper.insertSelective(user);
        return record;
    }
    @Override
    public User startLogin(String username, String psw) {
        UserExample userExample = new UserExample();
        Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(username);
        criteria.andUserPswEqualTo(psw);
        List<User> userList = userMapper.selectByExample(userExample);
        if(userList!=null&&userList.size()==1) {
            return userList.get(0);
        }
        return null;
    }

    @Override
    public int resetPsw(String userName, String oldPsw,String newPsw) {
        UserExample userExample = new UserExample();
        Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(userName);
        List<User> users = userMapper.selectByExample(userExample);
        if(users==null||users.size()==0){
            //用戶不存在
            return 2;
        }
        if(users.size()>1){
            return 0;
        }
        if(!oldPsw.equals(users.get(0).getUserPsw())){
            //原始密码不正确
            return 3;
        }

        User user = new User();
        user.setUserId(users.get(0).getUserId());
        user.setUserPsw(newPsw);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int newUserReset(String userName, String psw, String withdraw_psw) {
        if(userName==null||psw==null||withdraw_psw==null){
            return 0;
        }
        if(String.valueOf(withdraw_psw).length()!=6){
            return 2;
        }
        User user = new User();
        user.setUserName(userName);
        user.setWithdrawPsw(withdraw_psw);
        user.setUserPsw(psw);
        UserExample userExample = new UserExample();
        Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(userName);

        return userMapper.updateByExampleSelective(user,userExample);
    }

}
